﻿#region License

// // -------------------------------------------------------------------------------------
// // This information refers to CallCount.cs
// // 
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file (CallCount.cs) is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------

#endregion

#region Using Directives

using System;
using System.Reflection;
using InstrumentationLibrary;
using InstrumentationWrapper;
using Mono.Cecil;
using Mono.Cecil.Cil;

#endregion

namespace InstrumentAspect
{
    public class CallCount : IInstrumentationAspect
    {
        private InstrumentationAttribute _identifyingAttributeInstance = new CallCountAttribute();

        #region IInstrumentationAspect Members

        public InstrumentationAttribute IdentifyingAttribute
        {
            get { return _identifyingAttributeInstance; }
            set { _identifyingAttributeInstance = value; }
        }

        public void Instrument(MethodDefinition method)
        {
            MethodInfo aspectMethod = typeof (InstrumentationWrapperService).GetMethod("LogCount",
                                                                                       new Type[] {typeof (string)});

            MethodReference aspectReference = null;
            aspectReference = method.Module.Import(aspectMethod);
            //aspectReference = _assemblyDefinition.MainModule.Import(aspectMethod);

            //Get the existing instructions
            var existingInstructions = method.Body.Instructions.ToArray();
            method.Body.Instructions.Clear();

            //Add the parameter instruction first
            method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldstr,
                                                            (method.DeclaringType.FullName + "." + method.Name +
                                                             ".callcount").ToLower()));
            //Add the call the to the method
            method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, aspectReference));


            //Put back the existing instructions
            foreach (var existingInstruction in existingInstructions)
                method.Body.Instructions.Add(existingInstruction);
        }

        #endregion
    }
}